﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Microsoft.Practices.Prism.ViewModel;
using Upreader.Engine;
using System.ComponentModel.Composition;
using Microsoft.Practices.ServiceLocation;

namespace Upreader.Presentation.Client.ViewModel
{
    public class ViewModelBase<TViewModelDerived> : NotificationObject, IPartImportsSatisfiedNotification, IDisposable
        where TViewModelDerived : ViewModelBase<TViewModelDerived>
    {
        #region Fields

        private DownloadEngine _downloadEngine;

        #endregion // Fields

        #region Methods

        public void RaisePropertyChanged(Expression<Func<TViewModelDerived, object>> propertyGetter)
        {
            RaisePropertyChanged(propertyGetter);
        }

        public void BeginInvoke(Action action)
        {
            if (action == null)
                throw new ArgumentNullException("action");

            var dispatcher = System.Windows.Application.Current.Dispatcher;
            if (dispatcher.CheckAccess())
            {
                action();
            }
            else
            {
                dispatcher.BeginInvoke(action);
            }
        }

        public virtual void OnImportsSatisfied()
        {
        }

        #endregion // Methods

        #region Protected Properties

        [Import]
        protected DownloadEngine DownloadEngine
        {
            get;
            set;
        }

        [Import]
        public IServiceLocator ServiceLocator
        {
            get;
            set;
        }

        #endregion // Protected Properties

        #region Dispose

        public void Dispose() 
        {
            Dispose(true);
            GC.SuppressFinalize(this); 
        }

        protected virtual void Dispose(bool disposing) 
        {
        }

        // Use C# destructor syntax for finalization code.
        ~ViewModelBase()
        {
            // Simply call Dispose(false).
            Dispose (false);
        }
   

        #endregion // Dispose
    }
}
